<?php
namespace Home\Controller;
use Think\Controller;

/**
 * 所有需要登陆后查看的页面继承此Controller
 * @author Simon
 *
 */
class AuditBaseController extends Controller {
    
	public $promoter_en = array();
	
    public function _initialize(){
    	//验证当前用户是否登陆
    	$login_user_id = cookie(C('AdminConfig.login_cookie_name'));
    	if(empty($login_user_id)){
    		//未登录
    		redirect(C('UrlConfig.login_url'));
    		return;
    	}
    	
    	
    	//验证当前用户是否通过审核
    	$this->promoter_en = M('promoterInfo')->where("promoter_id='%s'", $login_user_id)->find();
  
    	if(empty($this->promoter_en)){
    		//不存在用户
    		redirect(C('UrlConfig.audit_error_url').'?type=1&promoter_id='.$login_user_id);
    		return;
    	}
    	
    	if($this->promoter_en['status'] == 0){
    		//用户待审核
    		redirect(C('UrlConfig.audit_error_url').'?type=2&promoter_id='.$login_user_id);
    		return;
    	}
    	
    	if($this->promoter_en['status'] == 2){
    		//用户审核拒绝
    		redirect(C('UrlConfig.audit_error_url').'?type=3&promoter_id='.$login_user_id);
    		return;
    	}
    	
    	$this->verifyPromoter();
    	
    	$this->assign('promoter_en', $this->promoter_en);
    	$this->addServerConfig();
    }
    
    
    /**
     * 验证请求参数中promoter_id和cookie中的promoter_id一致性
     */
    private function verifyPromoter(){
    	$promoter_id = I('promoter_id');
    	
    	$login_user_id = cookie(C('AdminConfig.login_cookie_name'));
    	$login_safecode_cookie = cookie(C('AdminConfig.login_safecode_cookie_name'));
    	
    	$safe_code = md5($login_user_id.C('SystemConfig.encrypt_key').date("y-m-d", time()));
    	
    	
    	
    	if($safe_code != $login_safecode_cookie){
    		//安全校验错误
    		redirect(C('UrlConfig.audit_error_url').'?type=22&promoter_id='.$login_user_id);
    		return;
    	}

    }
    
    
    
    private function addServerConfig(){
    	$ServerConfig = array();
    	$ServerConfig['AdminConfig'] = C('AdminConfig');
    	$ServerConfig['UrlConfig'] = C('UrlConfig');
    	 
    	$this->assign('ServerConfig', json_encode($ServerConfig));
    }
    
}